﻿Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Runtime.CompilerServices

Public Module Extension

    <Extension()> _
    Public Sub DeleteDirectory(ByVal [Me] As IsolatedStorageFile, ByVal dirName As String, ByVal recursive As Boolean)
        If recursive Then
            Dim stk As New Stack(Of String)
            stk.Push(dirName)
            While stk.Count > 0
                Dim currentPath As String = stk.Pop()
                Dim currentFiles = [Me].GetFileNames(IO.Path.Combine(currentPath, "*.*"))
                Dim currentDirs = [Me].GetDirectoryNames(IO.Path.Combine(currentPath, "*.*"))
                For Each fi In currentFiles
                    [Me].DeleteFile(IO.Path.Combine(currentPath, fi))
                Next
                If currentDirs.Length = 0 Then
                    [Me].DeleteDirectory(currentPath)
                End If
                For Each d In currentDirs
                    stk.Push(IO.Path.Combine(currentPath, d))
                Next
            End While
        Else
            [Me].DeleteDirectory(dirName)
        End If
    End Sub

    <Extension()> _
    Public Function RemoveIf(Of T)(ByVal [Me] As ICollection(Of T), ByVal predict As Func(Of T, Boolean)) As Boolean
        Try
            For Each item In [Me]
                If predict(item) Then
                    [Me].Remove(item)
                End If
            Next
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    <Extension()> _
    Public Function RemoveFirstIf(Of T)(ByVal [Me] As ICollection(Of T), ByVal predict As Func(Of T, Boolean)) As Boolean
        For Each item In [Me]
            If predict(item) Then
                [Me].Remove(item)
                Return True
            End If
        Next
        Return False
    End Function

    <Extension()> _
    Public Function Substring(ByVal [Me] As String, ByVal LeftChar As String, ByVal RightChar As String, Optional ByVal startIndex As Integer = 0) As String
        Dim posLeft = [Me].IndexOf(LeftChar, startIndex)
        Dim posRight = [Me].IndexOf(RightChar, posLeft + 1)
        Return [Me].Substring(posLeft + LeftChar.Length, posRight - posLeft - LeftChar.Length)
    End Function

    <Extension()> _
    Public Function RightOfLastChar(ByVal [Me] As String, ByVal lstChar As String) As String
        Dim pos = [Me].LastIndexOf(lstChar) + lstChar.Length
        Return [Me].Substring(pos)
    End Function

    ''' <summary>
    ''' Try to change the current resolution.
    ''' If failed, it keeps the original value.
    ''' </summary>
    ''' <param name="Me"></param>
    ''' <param name="size"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()> _
    Public Function TryChangeResolution(ByVal [Me] As GraphicsDeviceManager, ByVal size As Vector2, Optional ByRef ori As Vector2 = Nothing) As Boolean
        ori = New Vector2([Me].PreferredBackBufferWidth, [Me].PreferredBackBufferHeight)
        Try
            With [Me]
                .PreferredBackBufferHeight = CInt(size.Y)
                .PreferredBackBufferWidth = CInt(size.X)
            End With
            [Me].ApplyChanges()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

End Module